<?php
/**
 * Created by YuanXiShop.
 * User: YuanXi
 * Date: 2021/6/3 0003
 * Time: 1:26
 */

namespace admin;


use models\BaseDao;

class Link extends Admin
{
    function __construct()
    {
        parent::__construct();
        $this->assign('menumark','link');
    }
    function index()
    {
        //获取数据库操作对象
        $db = new BaseDao();
        //获取全部友情链接，并按ord排序
        $data = $db->select('link','*',['ORDER'=>['ord'=>'ASC','id'=>'DESC']]);
        //将数据分配给模板
        $this->assign('data',$data);
        //标题
        $this->assign('title','友情链接');
        //输出模板
        $this->display('link/index');
    }
    function add()
    {
        if($_POST['do_submit']){
            $db = new BaseDao();
            unset($_POST['do_submit']);
            if($db->insert('link',$_POST)){
                $this->success('/admin/link','添加成功！');
            }else{
                $this->error('/admin/link','添加失败！');
            }
        }
        $this->assign('title','友情链接添加');
        $this->display('link/add');
    }
    function mod($id)
    {
        $db = new BaseDao();
        $data = $db->get('link','*',['id'=>$id]);
        $this->assign('link',$data);
        $this->assign('title','修改友情连接');
        $this->display('link/mod');
    }
    function doupdate()
    {
        $id = $_POST['id'];
        unset($_POST['id']);
        $db = new BaseDao();
        if($db->update('link',$_POST,['id'=>$id])){
            $this->success('/admin/link','修改成功！');
        }else{
            $this->error('/admin/link/mod/'.$id,'修改失败！');
        }
    }
    function del($id)
    {
        $db = new BaseDao();
        if($db->delete('link',['id'=>$id])){
            $this->success('/admin/link','删除成功！');
        }else{
            $this->error('/admin/link','删除失败！');
        }
    }
    function order()
    {
        $db = new BaseDao();
        $num = 0;
        foreach ($_POST['ord'] as $id=>$ord){
            $num += $db->update('link',['ord'=>$ord],['id'=>$id]);
        }
        if($num > 0){
            $this->success('/admin/link','重新排序成功！');
        }else{
            $this->error('/admin/link','重新排序失败！');
        }
    }
}